package com.jin.Es;

import com.alibaba.fastjson.JSON;
import com.jin.Es.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class EsDemo03 {
    private RestHighLevelClient client;

    //1、创建SearchSourceBuilder对象
    private SearchSourceBuilder sourceBuilder;

    @Before
    public void init() {
        client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("192.168.200.129", 9200, "http")));
    }

    @After
    public void close() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void printResultByQuery(SearchSourceBuilder sourceBuilder) throws IOException {
        //3、创建SearchRequest,指定索引库
        SearchRequest request = new SearchRequest("user");
        request.source(sourceBuilder);
        //4、发送请求，得到响应结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //5、解析响应结果
        //6、hits代表查询结果的JSON对象
        SearchHits hits = response.getHits();

        System.out.println("total=" + hits.getTotalHits().value);
        SearchHit[] hitsHits = hits.getHits();

        for (SearchHit hit : hitsHits) {
            User user = JSON.parseObject(hit.getSourceAsString(), User.class);
            System.out.println(user);
        }
    }

    /**
     * 查询所有
     */
    @Test
    public void matchAllSearch() throws IOException {
        sourceBuilder = new SearchSourceBuilder();
        //2、添加matchAllQuery条件，matchAll
        sourceBuilder.query(QueryBuilders.matchAllQuery());

        printResultByQuery(sourceBuilder);


    }

    /**
     * 按照词条查询
     */
    @Test
    public void termQuery() throws IOException {
        sourceBuilder = new SearchSourceBuilder();
        //2、添加matchAllQuery条件，
        sourceBuilder.query(QueryBuilders.termQuery("name", "小红"));

        printResultByQuery(sourceBuilder);
    }

    /**
     * 分词查询
     */
    @Test
    public void matchQuery() throws IOException {
        sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchQuery("note", "唱歌 javaee"));

        printResultByQuery(sourceBuilder);
    }

    /**
     * 范围查询&排序查询
     */
    @Test
    public void rangeQuery() throws IOException {
        sourceBuilder = new SearchSourceBuilder();
        //构建范围查询对象
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
        //22<=age<27
        rangeQuery.gte(22);
        rangeQuery.lt(27);
        sourceBuilder.query(rangeQuery);
        //添加排序,倒序排序
        sourceBuilder.sort("id", SortOrder.ASC);
        printResultByQuery(sourceBuilder);
    }

    /**
     * 条件查询+过滤
     */
    @Test
    public void boolQuery() throws IOException {
        sourceBuilder = new SearchSourceBuilder();
        //构架BoolQueryBuilder对象
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("note", "同学");

        queryBuilder.must(matchQueryBuilder);

        //过滤性别为女‘gender’:"0"
        TermQueryBuilder gender = QueryBuilders.termQuery("gender", "0");
        queryBuilder.filter(gender);

        //过滤年龄在20-30
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age").gte(20).lte(30);
        queryBuilder.filter(rangeQuery);
        sourceBuilder.query(queryBuilder);
        printResultByQuery(sourceBuilder);
    }

    /**
     * 分页查询
     */
    @Test
    public void boolQueryByPage() throws IOException {
        sourceBuilder = new SearchSourceBuilder();
        //构架BoolQueryBuilder对象
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("note", "黑马");
        queryBuilder.must(matchQuery);

        sourceBuilder.query(queryBuilder);

        //设置分页
        int page = 1;
        int size = 2;
        // 每页的起始页
        int from = (page - 1) * size;

        sourceBuilder.from(from);
        sourceBuilder.size(size);

        printResultByQuery(sourceBuilder);
    }
}
